package kokist.android.shichaanimator;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import java.util.ArrayList;


/***
 *
 */
public class ParallaxContainer extends FrameLayout {

    private ArrayList<ParllaxFragemnt> fragments;
    private ParllaxFragmentPageAdapter adapter;

    public  ParallaxContainer(Context context){
       this(context,null);
   }

    public ParallaxContainer(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ParallaxContainer(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public ParallaxContainer(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }


    public void  setup(int... childids){
        //根据布局文件初始化，fragemet
       fragments= new ArrayList<ParllaxFragemnt>();
        for (int i = 0; i <childids.length ; i++) {
            ParllaxFragemnt fragemnt=new ParllaxFragemnt();
            Bundle bundle=new Bundle();
            //页面索引
            bundle.putInt("index",i);
            //布局id
            bundle.putInt("layoutid",childids[i]);
            fragemnt.setArguments(bundle);
            fragments.add(fragemnt);
        }
        //实例化适配器
       MainActivity mainActivity=(MainActivity)getContext();
        adapter=new ParllaxFragmentPageAdapter(mainActivity.getSupportFragmentManager(),fragments);
        ViewPager viewPager=new ViewPager(getContext());
        viewPager.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        viewPager.setId(R.id.container_viewpager);
        viewPager.setAdapter(adapter);
        addView(viewPager,0);
    }
}
